package com.king.oa.base;

import java.lang.reflect.ParameterizedType;

import javax.annotation.Resource;

import com.king.oa.domain.User;
import com.king.oa.service.DepartmentService;
import com.king.oa.service.ForumService;
import com.king.oa.service.PrivilegeService;
import com.king.oa.service.ReplyService;
import com.king.oa.service.RoleService;
import com.king.oa.service.TopicService;
import com.king.oa.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("unchecked")
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	private static final long serialVersionUID = -2950435517555420132L;

	// ============== ModelDriven的支持 ===================//
	
	protected T model;
	
	public BaseAction(){
		//使用反射技术得到T的真是类型
		try {
			ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();//获取当前new的对象的泛型的父类类型
			Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];//获取第一个类型参数的真是类型
			model = clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public T getModel() {
		return model;
	}

	// ============== Service实例的声明 ===================//
	@Resource
	protected DepartmentService departmentService;
	@Resource
	protected RoleService roleService;
	@Resource
	protected UserService userService;
	@Resource
	protected PrivilegeService privilegeService;
	@Resource
	protected ForumService forumService;
	@Resource
	protected TopicService topicService;
	@Resource
	protected ReplyService replyService;
	
	
	/**
	 * 获取当前登录的用户
	 * 
	 * @return
	 */
	protected User getCurrentUser() {
		return (User) ActionContext.getContext().getSession().get("user");
	}
	
	//================= 分页用的参数 ==================//
	
	protected int pageNum = 1;//当前页
	protected int pageSize = 20;//每页显示多少条
	
	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	
}
